// test bool是否为练习模式，finish退出界面
export function exitAF(flag){
    AFRAME.registerComponent("exitaf", {
        init: function () {
            if (flag){
                this.el.sceneEl.exitVR()
            }
        }
    })
}


export function player(test,visible){
    AFRAME.registerComponent("player", {
        init: function () {
            document.addEventListener('keyup', event => {
                // Jump
                if (event.code === 'Space') {
                    let player = document.querySelector("#player");
                    console.log(player.getAttribute("position"))
                    if (player.getAttribute("position").y <= 2) { // prevent successive jump
                        player.setAttribute('velocity', '0 12 0');
                    }
                }
            })

            this.el.addEventListener('collide', (evt) => {
                if (!test) {
                    console.log(evt.detail.body.el.id)
                    if (evt.detail.body.el.id != undefined && evt.detail.body.el.id == "finish-tile") {
                        console.log("finded!")
                        this.el.sceneEl.exitVR()
                        // 展示退出界面
                        visible.value = true;
                        // let finishDlgElm = document.querySelector("#finishDialog")
                        // finishDlgElm.setAttribute("style", "display: block");
                        // this.finished = true;
                    }
                }
            });
        }
    })

}
